package com.wjw;

import java.util.concurrent.atomic.AtomicReference;

/**
 * Singleton - CAS
 *
 * @author ikaros
 * @date 2024/7/18 2:06
 * @see <a href="ikaros@qq.com">mailTo: ikaros@qq.com</a>
 */
public class Singleton07 {

    private static final AtomicReference<Singleton07> INSTANCE = new AtomicReference<>();

    private static Singleton07 instance;

    private Singleton07() {
    }

    public static Singleton07 getInstance() {
        for (; ; ) {
            Singleton07 instance = INSTANCE.get();
            if (instance != null) {
                return instance;
            }
            INSTANCE.compareAndSet(null, new Singleton07());
            return INSTANCE.get();
        }
    }
}
